import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/devices/building_list_data.dart';
import 'package:ls_work/util/utils.dart';

import '../../ui/device/device_repair_submit_page.dart';

class DeviceRepairBuildingViewModel extends BaseViewModel {
  DeviceRepairBuildingViewModel(BuildContext context) : super(context);

  var buildingList = <IBuildingModel>[];

  Future<void> getBuildingList() async {
    showLoadingState();
    try{
      var data = await PostRequest.buildingList();
      if("0"==data['code'].toString()){
        buildingList = ((data['data'] as List?)??[]).map((e) => BuildingListData.fromJson(e)).toList();
        buildingList.insert(0, BuildingListData(buildingNo: '全部',));
        notifyListeners();
        showContent();
      }else{
        showErrorState(data['msg']);
      }
    }catch(e){
      showErrorState(NetErrorUtils.getNetError(e));
    }
  }

  Future<void> scanCode() async {
    var code = await Utils.scan();
    if(code!=null){

      Go().push(DeviceRepairSubmitPage(content: code,));
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    getBuildingList();
  }

}

abstract class IBuildingModel {
  String get iBuildingId;
  String get iBuildingName;
}


